package phapp.service.system.impl;

import java.util.*;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import javacommon.util.*;
import phapp.model.system.*;
import phapp.dao.system.*;
import phapp.service.common.QfdException;
import phapp.service.system.impl.*;
import phapp.service.system.*;

/**
 * Title: ts_sys_user 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("sysUserManager")
@Transactional
public class SysUserManagerImpl extends BaseManager<SysUser, java.lang.Integer> implements SysUserManager
{

   /** 定义Dao */
   private SysUserDao sysUserDao;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setSysUserDao(SysUserDao dao)
   {
      this.sysUserDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.sysUserDao;
   }

   /**
    * <p>
    * 分页查询SysUser<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return sysUserDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 SysUser<br>
    * 
    * @param SysUser
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = false)
   public Boolean removeByIdArry(SysUser u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = sysUserDao.removeByIdArry(u);
      if (i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   
   @Transactional
   public Map resetSysUserPassword(SysUser userData)
   {
      int nStatus = 0;
      String strPasswd;
      SysUser origData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      
      try
      {
         origData = (SysUser) sysUserDao.getById(userData.getUserId());
         if(origData == null)
            throw new QfdException(10001);
         
         strPasswd = userData.getUserPasswd();
         strPasswd = EncodeDecodeUtil.base64Encode(strPasswd);
         
         update(userData);
         nStatus = 0;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eReset)
      {
         nStatus = 9999;
         eReset.printStackTrace();
      }
      finally
      {
         if(nStatus == 10001)
            mapResult.put("msg", "用户不存在！");
         else if(nStatus == 10002)
            mapResult.put("msg", "原密码不正确！");
         else if(nStatus == 9999)
            mapResult.put("msg", "系统异常！");
         else
            mapResult.put("msg", "操作成功！");
         
         mapResult.put("success", nStatus == 0 ? true : false);
      }
      
      return mapResult;
   }

   @Transactional
   public Map updatePassword(SysUser userData)
   {
      int nStatus = 0;
      SysUser origData = null, newuserData = null;
      String strPasswd, strNewPasswd, strOrigPasswd, strVerifyPass;
      Map<String, Object> mapResult = new HashMap<String, Object>();

      try
      {
         strPasswd = userData.getUserPasswd();
         strNewPasswd = userData.getUserNote();

         origData = (SysUser) getById(userData.getId());
         if(origData == null)
            throw new QfdException(10001);
         
         strOrigPasswd = origData.getUserPasswd();
         strVerifyPass = EncodeDecodeUtil.base64Encode(strPasswd);
         if (!strOrigPasswd.equals(strVerifyPass))
            throw new QfdException(10002);

         newuserData = new SysUser();
         strNewPasswd = EncodeDecodeUtil.base64Encode(strNewPasswd);
         newuserData.setUserId(userData.getUserId());
         newuserData.setUserPasswd(strNewPasswd);
         sysUserDao.update(newuserData);
         nStatus = 0;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch (Exception eOther)
      {
         nStatus = 9999;
         eOther.printStackTrace();
      }
      finally
      {
         if(nStatus == 10001)
            mapResult.put("msg", "用户不存在！");
         else if(nStatus == 10002)
            mapResult.put("msg", "原密码不正确！");
         else if(nStatus == 9999)
            mapResult.put("msg", "系统异常！");
         else
            mapResult.put("msg", "操作成功！");
         
         mapResult.put("success", nStatus == 0 ? true : false);
      }

      return mapResult;
   }
   
   
   
}
